package de.velvettear.dimmdrivehelper.kernel;

import java.awt.MouseInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Vector;

import de.velvettear.dimmdrivehelper.gui.CoordinatePicker;

public class Main {
	
	private final static String CONFIG = "-config";

	private final static File INI = new File("DimmdriveHelper.ini");
	private final static String PARAM_APPLICATION = "APPLICATION=";
	private final static String PARAM_POSITION = "POSITION=";
	
	private final static String DIMMDRIVE = "dimmdrive-app.exe";
	private final static String EXE_DEFAULT = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Dimmdrive\\dimmdrive.exe";
		
	private final static Vector<Coordinate> POSITIONS = new Vector<Coordinate>();
			
	private final static String INI_TEXT = "--------------- Launch configuration for the DimmdriveHelper ---------------\n" + 
										   "-   APPLICATION=C:\\Path\\to\\your\\dimmdrive.exe                          -\n" + 
										   "-   POSITIONS=X,Y                                                          -\n" + 
										   "-   Use commandline argument \"-config\" to start the configuration wizard -\n" +
										   "----------------------------------------------------------------------------"   +
										   PARAM_APPLICATION + "\n" + 
										   PARAM_POSITION;
										   
	
	private static boolean waiting = true;
	
	private static boolean config = false;
	
	private static String exe = "";
	
	
	public static void main(String[] args) {		
		POSITIONS.add(new Coordinate(804, 394));
		POSITIONS.add(new Coordinate(2722, 414));
		
		initStructure();
		
		for (String arg : args) {
			if (arg.equals(CONFIG)) {
				config = true;
			}
		}
		
		if (config) {
			CoordinatePicker picker = new CoordinatePicker();
			picker.setVisible(true);
		}
		
		if (!isDimmdriveRunning()) {
			if (startDimmdrive()) {
				Thread activationThread = new Thread(new Runnable() {				
					@Override
					public void run() {
						while(waiting) {
							try {
								Thread.sleep(1000);
								if (isDimmdriveRunning()) {
									Thread.sleep(10000);
									clickButton();
									waiting = false;
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}					
					}
				});
				
				activationThread.start();
			}
		} else {
			Thread activationThread = new Thread(new Runnable() {				
				@Override
				public void run() {
					while(waiting) {
						try {
							Thread.sleep(1000);
							if (isDimmdriveRunning()) {
								Thread.sleep(10000);
								clickButton();
								waiting = false;
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}					
				}
			});
			
			activationThread.start();
		}
	}
	
	private static void initStructure() {
		if (INI.exists()) {
			
		} else {
			try {				
				BufferedWriter writer = new BufferedWriter(new FileWriter(INI));
				writer.write(INI_TEXT);				
			} catch (Exception e) {
				e.printStackTrace();
			}			
		}
	}
	
	private static boolean startDimmdrive() {
		boolean started = false;
//		
//		try {
//			if (new File(EXE).exists()) {
//				Runtime.getRuntime().exec(EXE);
//				started = true;
//			}			
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
		return started;
	}
	
	private static boolean isDimmdriveRunning() {		
		boolean running = false;
		
		String line;
		try {
			Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
			BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
	        while ((line = in.readLine()) != null) {
	            if (line.contains(DIMMDRIVE)) {
	            	running = true;
	            }
	        }
	        in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("Dimmdrive running: " + running);
		
		return running;
	}
	
	private static void clickButton() {
		try {					
			for (Coordinate c : POSITIONS) {
				int currentX = MouseInfo.getPointerInfo().getLocation().x;
				int currentY = MouseInfo.getPointerInfo().getLocation().y;
				
				Robot bot = new Robot();
				bot.mouseMove(c.x, c.y);    
				bot.mousePress(InputEvent.BUTTON1_MASK);
				bot.mouseRelease(InputEvent.BUTTON1_MASK);
				
				System.out.println("Executed click: " + c.x + ", " + c.y);
				
				bot.mouseMove(currentX, currentY);   
			}				
		} catch (Exception e) {
			e.printStackTrace();
		}			
	}
	
}
